package com.lc.rocketmq.dao;

import com.lc.rocketmq.po.Goods;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

/**
 * @author liuc
 * @date 2021/8/21
 * @description
 **/
public interface GoodsDao extends JpaRepository<Goods,Long> {

    @Modifying
    @Query(nativeQuery = true, value = "update goods set stock = stock + ?2 where id = ?1 and version = ?3 and stock >= 0")
    int reduceStock(Long id,int stock, int version);

    @Query(nativeQuery = true, value = "select * from goods where id = ?1 for update")
    Goods forUpadate(Long id);
}
